﻿/*!
 * jQuery Countdown plugin v1.0
 * http://www.littlewebthings.com/projects/countdown/
 *
 * Copyright 2010, Vassilis Dourdounis
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * 倒计时插件修改 
 *
 */
 
 function countDown(id){
 
		diffSecs = setCountDown(id);

		$('#' + id + ' .digit').html('<div class="countdowntop"></div><div class="countdownbottom"></div>');
		doCountDown(id, diffSecs, 500);	
 }

 function setCountDown(id) {
     var targetTime = new Date(2013, 5, 24, 13, 29, 59, 99);
     //targetTime.setFullYear('2013');
     //targetTime.setMonth('5');
     //targetTime.setDate('24');
     //targetTime.setHours('13');
     //targetTime.setMinutes('29');
     //targetTime.setSeconds('59');

     var nowTime = new Date();
     //nowTime.setFullYear('2014');
     nowTime.setFullYear($('#nowyear').val());
     var nowmonth = $('#nowmonth').val();

     if (nowmonth != '') {
         nowmonth = parseInt(nowmonth);         
         nowmonth--;
         nowTime.setMonth(nowmonth);
     }
     nowTime.setDate($('#nowday').val());
     nowTime.setHours($('#nowhour').val());
     nowTime.setMinutes($('#nowminute').val());
     nowTime.setSeconds($('#nowsecond').val());

     if ((nowTime.valueOf() - new Date(2013, 5, 24, 13, 29, 59, 99)) > 0) {
         targetTime = new Date(2013, 6, 2, 17, 59, 59, 99);
         $('#countdown_tip_pre').html("评分结束倒计时：");
     }

     if (targetTime.valueOf() - nowTime.valueOf() < 0) {
         $('#countdown_tip_pre').html("评分结束，不能评分！");
         $('#' + id).html('');
     }


     diffSecs = Math.floor((targetTime.valueOf() - nowTime.valueOf()) / 1000);

     $('#' + id).data('diffSecs', diffSecs);

     return diffSecs;
 };
	
	
function doCountDown (id, diffSecs, duration) {
		var countdownthis= $('#' + id);
		if (diffSecs <= 0)
		{
			diffSecs = 0;
			if ($('#' + id).data('timer'))
			{
				clearTimeout($('#' + id).data('timer'));
			}
		}

		secs = diffSecs % 60;
		mins = Math.floor(diffSecs/60)%60;
		hours = Math.floor(diffSecs/60/60)%24;

		days = Math.floor(diffSecs/60/60/24);
		weeks = Math.floor(diffSecs/60/60/24/7);


		dashChangeTo(id, 'seconds_dash', secs, duration ? duration : 800);
		dashChangeTo(id, 'minutes_dash', mins, duration ? duration : 1200);
		dashChangeTo(id, 'hours_dash', hours, duration ? duration : 1200);
		dashChangeTo(id, 'days_dash', days, duration ? duration : 1200);
		dashChangeTo(id, 'weeks_dash', weeks, duration ? duration : 1200);

		$('#' + id).data('diffSecs', diffSecs);
		if (diffSecs > 0)
		{
			//e = $('#' + id);
			t = setTimeout(function() { doCountDown(id, diffSecs-1) } , 1000);
			$('#' + id).data('timer', t);
		} 
		else if (cb = $('#' + id).data('callback')) 
		{
			$('#' + id).data('callback')();
		}

	};

function dashChangeTo (id, dash, n, duration) {
		  var countdownthis = $('#' + id);
		 
		  for (var i=(countdownthis.find('.' + dash + ' .digit').length-1); i>=0; i--)
		  {
				var d = n%10;
				n = (n - d) / 10;
				digitChangeTo('#' + countdownthis.attr('id') + ' .' + dash + ' .digit:eq('+i+')', d, duration);
		  }
	};
	
	
function digitChangeTo (digit, n, duration) {
		if (!duration)
		{
			duration = 800;
		}
		if ($(digit + ' div.countdowntop').html() != n + '')
		{

			$(digit + ' div.countdowntop').css({'display': 'none'});
			$(digit + ' div.countdowntop').html((n ? n : '0')).slideDown(duration);

			$(digit + ' div.countdownbottom').animate({'height': ''}, duration, function() {
				$(digit + ' div.countdownbottom').html($(digit + ' div.countdowntop').html());
				$(digit + ' div.countdownbottom').css({'display': 'block', 'height': ''});
				$(digit + ' div.countdowntop').hide().slideUp(10);

			
			});
		}
	};
	
$(document).ready(function(){
    countDown('countdown_dashboard');
});